package com.henry.server;

import com.henry.model.UserMessage;

import java.io.IOException;

/*
*   编程实现服务器的主功能
* */
public class ServerView {
    private ServerDao sd;

    public ServerView() {
    }

    public ServerView(ServerDao sd) {
        this.sd = sd;
    }

    public void serverReceive(ServerThread st) throws IOException, ClassNotFoundException {
        UserMessage userMessage = (UserMessage) st.getOis().readObject();
        if(userMessage.getType().equals("end"))
            return;
        System.out.println(userMessage.toString());
        switch (userMessage.getType()) {
            case "managerCheck" :
                check(st, userMessage, sd.serverManagerCheck(userMessage.getUser()));
                break;
            case "studentCheck" :
                check(st, userMessage, sd.serverStudentCheck(userMessage.getUser()));
                break;
            case "addStudent" :
                check(st, userMessage, sd.addStudentCheck(userMessage.getUser()));
                break;
            case "deleteStudent" :
                check(st, userMessage, sd.deleteStudentCheck(userMessage));
                break;
            case "reviseStudent" :
                check(st, userMessage, sd.reviseStudentCheck(userMessage.getUser()));
                break;
            case "findStudent" :
                st.getOos().writeObject(sd.findStudent(userMessage));
                break;
            case "revisePassword":
                check(st, userMessage, sd.revisePassword(userMessage.getUser()));
                break;
            case "getQuestion":
                st.getOos().writeObject(sd.getQuestion());
                break;
            case "postGrade":
                sd.postGrade(userMessage);
                break;
            case "outputGrade":
                sd.outputGrade();
            default:
        }
    }

    public void check(ServerThread st,UserMessage userMessage, boolean b) throws IOException {
        if(b) {
            userMessage.setType("success");
        }else {
            userMessage.setType("fail");
        }
        st.getOos().writeObject(userMessage);
    }
}
